<template>
  <div ref="printTemplate" class="print-template">
    <div class="print-header">
      <h2>烟叶育苗配送单</h2>
      <div class="print-info">
        <span>打印时间：{{ printTime }}</span>
        <span>打印人：{{ printUser }}</span>
      </div>
    </div>

    <div class="order-info">
      <h3>订单信息</h3>
      <table>
        <tr>
          <th width="120">订单编号</th>
          <th width="200">育苗点</th>
          <th width="100">总数量(亩)</th>
          <th width="100">总盘数</th>
          <th>配送日期</th>
        </tr>
        <tr>
          <td>{{ order.orderId }}</td>
          <td>{{ order.deptName }}</td>
          <td>{{ order.totalQuantity }}</td>
          <td>{{ order.totalPlateQuantity }}</td>
          <td>{{ order.deliveryDate }}</td>
        </tr>
      </table>
    </div>

    <div class="driver-info">
      <h3>司机信息</h3>
      <table>
        <tr>
          <th width="100">司机姓名</th>
          <th width="150">联系电话</th>
          <th>车牌号</th>
        </tr>
        <tr>
          <td>{{ order.driverName }}</td>
          <td>{{ order.driverPhone }}</td>
          <td>{{ order.licensePlate }}</td>
        </tr>
      </table>
    </div>

    <div class="demand-list">
      <h3>需求明细</h3>
      <table>
        <thead>
          <tr>
            <th width="40">序号</th>
            <th width="80">农户姓名</th>
            <th width="120">联系电话</th>
            <th width="120">农户地址</th>
            <th width="100">烟苗品种</th>
            <th width="80">需求数量(亩)</th>
            <th width="60">票盘数</th>
            <th width="100">需求日期</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(demand, index) in order.demands" :key="demand.demandId">
            <td>{{ index + 1 }}</td>
            <td>{{ demand.growerName }}</td>
            <td>{{ demand.phone }}</td>
            <td>{{ demand.townName }}{{ demand.villageGroupName }}</td>
            <td>{{ demand.seedlingType }}</td>
            <td>{{ demand.quantity }}</td>
            <td>{{ demand.plateQuantity }}</td>
            <td>{{ demand.demandTime }}</td>
          </tr>
        </tbody>
      </table>
    </div>

    <div class="footer">
      <div class="signature-area">
        <div class="signature-line">
          <span>司机签名：</span>
          <span class="signature-box"></span>
        </div>
        <div class="signature-line">
          <span>育苗点签名：</span>
          <span class="signature-box"></span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PrintOrderTemplate',
  props: {
    order: {
      type: Object,
      required: true
    },
    printUser: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      printTime: new Date().toLocaleString()
    }
  }
}
</script>

<style lang="scss" scoped>
.print-template {
  padding: 0;
  font-size: 14px;
  background-color: white;
  box-sizing: border-box;
  
  @media print {
    padding: 0;
    margin: 0;
    width: 210mm;  // A4纸宽度
    min-height: 297mm; // A4纸高度
  }
}

.print-header {
  text-align: center;
  margin: 0 0 15px 0;
  padding-top: 10px;
  
  h2 {
    margin: 0 0 8px 0;
    font-size: 18px;
  }
  
  .print-info {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #666;
    padding: 0 20px;
  }
}

.order-info, .driver-info {
  margin: 0 20px 15px 20px;
  
  h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
  }
  
  table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    
    th, td {
      border: 1px solid #000;
      padding: 6px 4px;
      text-align: center;
      font-size: 12px;
      white-space: normal;
      word-break: break-all;
      line-height: 1.3;
    }
    
    th {
      background-color: #f8f8f8;
      font-weight: bold;
    }
  }
}

.demand-list {
  margin: 0 20px 15px 20px;
  
  h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
  }
  
  table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    
    th, td {
      border: 1px solid #000;
      padding: 6px 4px;
      text-align: center;
      font-size: 12px;
      white-space: normal;
      word-break: break-all;
      line-height: 1.3;
    }
    
    th {
      background-color: #f8f8f8;
      font-weight: bold;
    }
  }
}

.footer {
  margin: 20px;
  page-break-inside: avoid;
  
  .signature-area {
    display: flex;
    justify-content: space-between;
    
    .signature-line {
      .signature-box {
        display: inline-block;
        width: 150px;
        margin-left: 10px;
        border-bottom: 1px solid #000;
        height: 24px;
      }
    }
  }
}

@media print {
  @page {
    size: A4;
    margin: 10mm;
  }

  .print-template {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .order-info, .driver-info, .demand-list {
    table {
      page-break-inside: auto;
      
      tr {
        page-break-inside: avoid;
      }
      
      th {
        background-color: #f8f8f8 !important;
      }
    }
  }
}
</style> 